package com.example.mybatis;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Scanner;

@SpringBootApplication
public class MybatisGenApplication {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    private static GlobalConfig globalConfig(){
        return new GlobalConfig().setOpen(false).setFileOverride(true).setBaseResultMap(true).setBaseColumnList(true).setAuthor("fair")
                .setEntityName("%sDO").setServiceName("%sService");
    }

    private static DataSourceConfig dataSourceConfig() {
        return new DataSourceConfig().setDriverName("com.mysql.jdbc.Driver").setUrl("jdbc:mysql://localhost:3306/citybrain?useUnicode=true&useSSL=false&characterEncoding=utf8")
                .setUsername("root").setPassword("123456");
    }

    private static StrategyConfig strategyConfig(){

        StrategyConfig strategyConfig = new StrategyConfig()
                // 表名生成策略：下划线连转驼峰
                .setNaming(NamingStrategy.underline_to_camel)
                // 表字段生成策略：下划线连转驼峰
                .setColumnNaming(NamingStrategy.underline_to_camel)
                // 生成controller
                .setRestControllerStyle(true)
                // controller映射地址：驼峰转连字符
                .setControllerMappingHyphenStyle(false)
                // 是否启用builder 模式
                .setChainModel(true)
                // 是否为lombok模型; 需要lombok依赖
                .setEntityLombokModel(true)
                // 生成实体类字段注解
                .setEntityTableFieldAnnotationEnable(true);
        return strategyConfig;
    }


    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();

        autoGenerator.setStrategy(strategyConfig());
        autoGenerator.setGlobalConfig(globalConfig());
        autoGenerator.setDataSource(dataSourceConfig());

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("fair");
        globalConfig.setOpen(false);
        autoGenerator.setGlobalConfig(globalConfig);

        PackageConfig pc = new PackageConfig();
        pc.setModuleName(scanner("模块名"));
        pc.setParent("com");
        autoGenerator.setPackageInfo(pc);

        autoGenerator.execute();
    }
}
